﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;

public partial class PrikazSlika : System.Web.UI.Page
{
    private int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        id = Convert.ToInt32(Request.QueryString["id"]);        
        pecati_gi_detalite_za_slikata(id);
        proveri_br_komentari(id);
        multi1.ActiveViewIndex = 0;
        if (Session["username"] == null)
        {
            LinkButton2.Enabled = false;
            LinkButton2.Visible = false;
        }
    }
    protected void pecati_gi_detalite_za_slikata(int id)
    {
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;
      
        
        //------------------------------- GI PRIKAZUVA DETALITE ZA SLIKATA -------------------------------

        string sqlQuestion = "SELECT * FROM Photos,Photographers WHERE ID=@ID";

        SqlCommand komanda = new SqlCommand(sqlQuestion, myConnection);
        komanda.Parameters.AddWithValue("@ID", id);
        SqlDataReader citac;

        try
        {
            myConnection.Open();
            citac = komanda.ExecuteReader();
            while (citac.Read())
            {
                lbl_det_photograper.Text = vrati_ime_i_prezime((string)citac["username"]);
                lbl_det_name.Text = citac["name"].ToString();
                lbl_det_location.Text = citac["location_taken"].ToString();
                lbl_det_equipment.Text = citac["equipment"].ToString();
                lbl_det_exposure.Text = citac["exposure"].ToString();
                lbl_det_points.Text = citac["points"].ToString();
                lbl_det_votes.Text = citac["votes"].ToString();
                img_field.ImageUrl = citac["image"].ToString();
                
            }
        }
        catch (Exception err)
        {
            
            myConnection.Close();
            return;
        }
        finally
        {
            myConnection.Close();
        }
    }
    protected string vrati_ime_i_prezime(string username)
    {
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;

        string sqlQuestion1 = "SELECT * FROM Photographers WHERE username=@username";

        SqlCommand komanda1 = new SqlCommand(sqlQuestion1, myConnection);
        komanda1.Parameters.AddWithValue("@username", username);

        SqlDataReader citac1;
        string ime_prezime = "";
        try
        {
            myConnection.Open();
            citac1 = komanda1.ExecuteReader();
            while (citac1.Read())
            {
                ime_prezime = citac1["first_name"].ToString() + " " + citac1["last_name"].ToString();
            }
        }
        catch (Exception err)
        {            
            myConnection.Close();
        }
        finally
        {
            myConnection.Close();
        }
        if (ime_prezime == "")
            return "unknown";
        else
            return ime_prezime;
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        multi1.ActiveViewIndex = 0;
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        if (proveri_dali_vekje_komentiral(id) == true)
            rbl_rate.Enabled = false;
        if (proveri_dali_e_sopstvenik(id) == true)
            rbl_rate.Enabled = false;
        rbl_rate.SelectedIndex = -1;
        txt_comment.Text = "";
        multi1.ActiveViewIndex = 1;
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        pecatenje_komentari(id);
        multi1.ActiveViewIndex = 2;
    }
    protected void btn_comment_Click(object sender, EventArgs e)
    {
        if (txt_comment.Text.Length < 300)
        {
            int bod = rbl_rate.SelectedIndex + 1;

            SqlConnection myConnection = new SqlConnection();
            myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;


            string sqlQuestion = "UPDATE Photos SET votes=votes+@votes, points=points+" + bod + " WHERE ID=" + id;

            SqlCommand komanda = new SqlCommand(sqlQuestion, myConnection);
            if (rbl_rate.SelectedIndex == -1)
                komanda.Parameters.AddWithValue("@votes", 0);
            else
                komanda.Parameters.AddWithValue("@votes", 1);

            int updated = 0;
            try
            {
                myConnection.Open();
                updated = komanda.ExecuteNonQuery();
                lbl_comment_result.Text = updated.ToString() + " record updated.";
            }
            catch (Exception err)
            {
                lbl_comment_result.Text = "Error updating photo points. ";
                lbl_comment_result.Text += err.Message;
            }
            finally
            {
                myConnection.Close();
            }


            string insertSql = "INSERT INTO Comment (ID,username,tekst,datum) VALUES (@ID,@username,@tekst,@datum)";
            SqlCommand cmd = new SqlCommand(insertSql, myConnection);
            cmd.Parameters.AddWithValue("@ID", id);
            cmd.Parameters.AddWithValue("@username", (string)Session["username"]);
            cmd.Parameters.AddWithValue("@tekst", txt_comment.Text);
            cmd.Parameters.AddWithValue("@datum", DateTime.Now.Date.ToShortDateString());
            int added = 0;
            try
            {
                myConnection.Open();
                added = cmd.ExecuteNonQuery();
                lbl_comment_result.Text = added.ToString() + " comment inserted.";
                pecati_gi_detalite_za_slikata(id);
                proveri_br_komentari(id);
                rbl_rate.Enabled = false;
            }
            catch (Exception err)
            {
                lbl_comment_result.Text = "Error inserting comment. ";
                lbl_comment_result.Text += err.Message;
            }

        }
        else
            lbl_comment_result.Text = "Comment lenght exceeds!";
    }
    //protected void pecati_gi_detalite_za_slikata(int id)
    //{
    //    SqlConnection myConnection = new SqlConnection();
    //    myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;


    //    //------------------------------- GI PRIKAZUVA DETALITE ZA SLIKATA -------------------------------

    //    string sqlQuestion = "SELECT * FROM Photos,Photographers WHERE ID=@ID";

    //    SqlCommand komanda = new SqlCommand(sqlQuestion, myConnection);
    //    komanda.Parameters.AddWithValue("@ID",id);
    //    SqlDataReader citac;



    //    try
    //    {
    //        myConnection.Open();
    //        citac = komanda.ExecuteReader();
    //        while (citac.Read())
    //        {
    //            lbl_det_photograper.Text = vrati_ime_i_prezime((string)citac["username"]);
    //            lbl_det_name.Text = citac["name"].ToString();
    //            lbl_det_location.Text = citac["location_taken"].ToString();
    //            lbl_det_equipment.Text = citac["equipment"].ToString();
    //            lbl_det_exposure.Text = citac["exposure"].ToString();
    //            lbl_det_points.Text = citac["points"].ToString();
    //            lbl_det_votes.Text = citac["votes"].ToString();
    //            //lbl_result.Text = citac["name"].ToString() + " " + citac["location_taken"].ToString();
    //        }
    //    }
    //    catch (Exception err)
    //    {
            
    //        myConnection.Close();
    //        return;
    //    }
    //    finally
    //    {
    //        myConnection.Close();
    //    }
    //}
    protected void proveri_br_komentari(int id)
    {
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;

        int broj_komentari = 0;
        string sqlQuestion2 = "SELECT * FROM Comment WHERE ID=" + id;

        SqlCommand komanda2 = new SqlCommand(sqlQuestion2, myConnection);

        SqlDataReader citac2;
        try
        {
            myConnection.Open();
            citac2 = komanda2.ExecuteReader();
            while (citac2.Read())
            {
                broj_komentari++;
            }
            lbl_nc.Text = "(" + broj_komentari.ToString() + ")";
        }
        catch (Exception err)
        {
            
            
            myConnection.Close();
            return;
        }
        finally
        {
            myConnection.Close();
        }

    
    }
    protected void pecatenje_komentari(int id)
        {
            lbl_show_comments.Text = "";
            SqlConnection myConnection = new SqlConnection();
            myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;

            string sqlQuestion2 = "SELECT * FROM Comment WHERE ID=@ID";

            SqlCommand komanda2 = new SqlCommand(sqlQuestion2, myConnection);
            komanda2.Parameters.AddWithValue("@ID", id);

            SqlDataReader citac2;
            try
            {
                myConnection.Open();
                citac2 = komanda2.ExecuteReader();
                while (citac2.Read())
                {

                    lbl_show_comments.Text += "<tr>";
                    lbl_show_comments.Text += "<td >" + vrati_ime_i_prezime((string)citac2["username"]) + "</td>";
                    lbl_show_comments.Text += "<td >" + Convert.ToString(citac2["datum"]) + "</td>";
                    lbl_show_comments.Text += "<td>" + (string)citac2["tekst"] + "</td>";
                    lbl_show_comments.Text += "</tr>";
                }

            }
            catch (Exception err)
            {
                lbl_show_comments.Text += "error reading comentari ";
                lbl_show_comments.Text += err.Message;
                myConnection.Close();
            }
            finally
            {
                myConnection.Close();
            }
        }
    protected bool proveri_dali_e_sopstvenik(int id)
        {
            SqlConnection myConnection = new SqlConnection();
            myConnection.ConnectionString = @"Data Source=localhost\SQLEXPRESS;" + "Initial Catalog=Photographers;Integrated Security=SSPI";

            string sqlQuestion2 = "SELECT * FROM Photos WHERE ID=@ID AND username=@username";

            SqlCommand komanda2 = new SqlCommand(sqlQuestion2, myConnection);
            komanda2.Parameters.AddWithValue("@username", (string)Session["username"]);
            komanda2.Parameters.AddWithValue("@ID", id);
            int indikator = 0;
            SqlDataReader citac2;
            try
            {
                myConnection.Open();
                citac2 = komanda2.ExecuteReader();
                while (citac2.Read())
                {
                    indikator = 1;
                    lbl_comment_result.Text += "<br/>";
                    lbl_comment_result.Text += vrati_ime_i_prezime((string)Session["username"]) + " is the owner!";
                }

            }
            catch (Exception err)
            {
                lbl_comment_result.Text += "error reading comentari ";
                lbl_comment_result.Text += err.Message;
                myConnection.Close();
            }
            finally
            {
                myConnection.Close();
            }
            if (indikator == 0)
                return false;
            else
                return true;

        }
    protected bool proveri_dali_vekje_komentiral(int id)
    {
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = @"Data Source=localhost\SQLEXPRESS;" + "Initial Catalog=Photographers;Integrated Security=SSPI";

        string sqlQuestion2 = "SELECT * FROM Comment WHERE ID=@ID AND username=@username";

        SqlCommand komanda2 = new SqlCommand(sqlQuestion2, myConnection);
        komanda2.Parameters.AddWithValue("@username", (string)Session["username"]);
        komanda2.Parameters.AddWithValue("@ID", id);
        int indikator = 0;
        SqlDataReader citac2;
        try
        {
            myConnection.Open();
            citac2 = komanda2.ExecuteReader();
            while (citac2.Read())
            {
                indikator = 1;
                lbl_comment_result.Text = vrati_ime_i_prezime((string)Session["username"]) + " has already rated this Photo!";
            }

        }
        catch (Exception err)
        {
            lbl_comment_result.Text += "error reading comentari ";
            lbl_comment_result.Text += err.Message;
            myConnection.Close();
        }
        finally
        {
            myConnection.Close();
        }
        if (indikator == 0)
            return false;
        else
            return true;

    }
    
}
